package com.example.lijuan.myapplication.UI.Fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.Button;

import android.widget.ListView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMapOptions;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.MyTrafficStyle;
import com.example.lijuan.myapplication.Adapter.CarInfoListAdapter;
import com.example.lijuan.myapplication.R;
import com.example.lijuan.myapplication.UI.Activity.DashboardApp;
import com.example.lijuan.myapplication.Util.Constants;

import java.util.ArrayList;

public class FragmentCarInfo extends Fragment {
    View rooView;
    private MapView mapView;

    protected AMap aMap;

    private Marker myMarker;
    private Marker deviceMarker;

    private Button btnTripHistory;
    private FragmentManager fragmentManager;

    private ListView listView = null;
    private ArrayList<Integer> list;


    private static final CameraPosition CAMERA_POSITION_TIANJIN = new CameraPosition.Builder()
            .target(Constants.TIANJIN).zoom(Constants.LOCATION_ZOOM).bearing(0).tilt(30).build();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null == fragmentManager) {
            fragmentManager = getFragmentManager();
        }
    }

    @Override
    public void onResume() {
        if (mapView != null) {
            mapView.onResume();
        }
        super.onResume();
    }

    @Override
    public void onPause() {

        if (mapView != null) {
            mapView.onPause();
        }
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mapView != null) {
            mapView.onSaveInstanceState(outState);
        }
    }

    @Override
    public void onDestroy() {
        if (mapView != null) {
            mapView.onDestroy();
        }
        super.onDestroy();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rooView = inflater.inflate(R.layout.fragment_car_info, container, false);
        mapView = (MapView) rooView.findViewById(R.id.map);
        listView = (ListView) rooView.findViewById(R.id.list);
        list = new ArrayList<>();
        list.add(50);
        list.add(80);
        list.add(100);
        listView.setAdapter(new CarInfoListAdapter(getActivity().getApplicationContext(), list));
        listView.setSelection(0);
        mapView.onCreate(savedInstanceState);


        initMap();
        setUpSpeed();
        drawDeviceMarker(Constants.TIANJIN);

        return rooView;
    }




    protected void initMap() {

        if (aMap == null) {
            aMap = mapView.getMap();
            setMapUi();
        }
        setTraffic();

    }

    private void setUpSpeed() {

        new showData().execute();


    }

    public void drawDeviceMarker(LatLng latLng) {
        if (deviceMarker == null) {
            BitmapDescriptor icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE);
            if (icon != null) {
                deviceMarker = aMap.addMarker(new MarkerOptions().draggable(false).icon(icon).position(latLng));
            }
        }
        if (latLng.longitude != 0.0 && latLng.latitude != 0.0) {
            deviceMarker.setPosition(latLng);

            aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
        }
    }

    private void setMapUi() {
        UiSettings mUiSettings = aMap.getUiSettings();
        mUiSettings.setZoomControlsEnabled(true);
        mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_LEFT);

        CameraUpdate update = CameraUpdateFactory.newCameraPosition(CAMERA_POSITION_TIANJIN);
        aMap.moveCamera(update);
    }

    private void setTraffic() {
        MyTrafficStyle myTrafficStyle = new MyTrafficStyle();
        myTrafficStyle.setSeriousCongestedColor(0xff92000a);
        myTrafficStyle.setCongestedColor(0xffea0312);
        myTrafficStyle.setSlowColor(0xffff7508);
        myTrafficStyle.setSmoothColor(0xff00a209);
        aMap.setTrafficEnabled(false);
    }

    private class showData extends AsyncTask<Byte, Byte, Byte> {

        @Override
        protected Byte doInBackground(Byte... params) {

            byte[] rtdata = DashboardApp.readRealTimeData();
            if (rtdata != null) {
                String s = new String(rtdata);
                Log.i("data :-", "" + s);
                //  showData(rtdata);


            }

            return null;
        }
    }

}
